package com.atguigu.tingshu.search.task;

import com.atguigu.tingshu.album.client.AlbumClientFeign;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.search.AlbumInfoIndex;
import com.atguigu.tingshu.search.service.SearchService;
import com.atguigu.tingshu.vo.search.AlbumInfoIndexVo;
import com.xxl.job.core.handler.annotation.XxlJob;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/***
 * 排行榜定时任务
 */
@Component
public class SearchRankTask {

    @Autowired
    private SearchService searchService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Resource
    private AlbumClientFeign albumClientFeign;


    /**
     * 排行榜数据预热
     */
    @XxlJob("searchRankTask")
    public void searchRankTask(){
        // 声明排行榜项目
        String[] items = new String[]{"hotScore", "playStatNum", "subscribeStatNum", "buyStatNum", "commentStatNum"};
        // feign查询所有一级分类列表
        List<BaseCategory1> baseCategory1List = albumClientFeign.getAllBaseCategory1();
        // 遍历
        baseCategory1List.stream().forEach(baseCategory1 ->{
            for (String item : items){
                List<AlbumInfoIndex> albumInfoIndexList = searchService.rank(baseCategory1.getId()+"",item);
                List<AlbumInfoIndexVo> albumInfoIndexVoList = albumInfoIndexList.stream().map(albumInfoIndex -> {
                    AlbumInfoIndexVo albumInfoIndexVo = new AlbumInfoIndexVo();
                    BeanUtils.copyProperties(albumInfoIndex, albumInfoIndexVo);
                    return albumInfoIndexVo;
                }).collect(Collectors.toList());
                redisTemplate.opsForHash().put(baseCategory1.getId()+"",item,albumInfoIndexVoList);
            }
        });
    }

    /**
     * 测试xxl-job定时任务
     */
    @XxlJob("testXxlJobTask")
    public void testXxlJobTask(){
        System.out.println("testXxlJobTask...");
    }

}
